home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.06 Jun 93 / TReportPrinter / TDemoDocument.cp < prev    next >
Encoding:
Text File  |  1992-11-01  |  4.3 KB  |  146 lines  |  [TEXT/MPS ]

  1. #include "TDemoDocument.h"
  2.  
  3. const CStr255 kTheLineToPrint = "Another amazing MacApp program!";
  4.  
  5. //----------------------------------------------------------------------------------------
  6. // TDemoDocument::Initialize: 
  7. //----------------------------------------------------------------------------------------
  8. #pragma segment MAOpen
  9. pascal void TDemoDocument::Initialize()
  10. {
  11.     inherited::Initialize();
  12.     fLineCount = 0;
  13.     fReportPrinter = nil;
  14.     fLineCountText = nil;
  15. }
  16. //----------------------------------------------------------------------------------------
  17. // TDemoDocument::IDemoDocument: 
  18. //----------------------------------------------------------------------------------------
  19. #pragma segment MAOpen
  20. pascal void TDemoDocument::IDemoDocument(TFile* itsFile, OSType itsScrapType)
  21. {
  22.     FailInfo    fi;
  23.     
  24.     this->IFileBasedDocument(itsFile, itsScrapType);
  25.     
  26.     fSaveUserSelection = false;     // Don't have one so there's nothing to save
  27.     
  28.     if(fi.Try()) {
  29.         fReportPrinter = new TReportPrinter;
  30.         fReportPrinter->IReportPrinter("\pmonaco",    // Font
  31.                         normal,                     // Face
  32.                         9,                             // Size
  33.                         72,                         // Left margin
  34.                         648,                         // Bottom Margin
  35.                         true);                        // do auto-FF
  36.         fi.Success();
  37.     }
  38.     else {
  39.         fReportPrinter = (TReportPrinter *)FreeIfObject(fReportPrinter);
  40.         fi.ReSignal();
  41.     }
  42. }
  43.  
  44.  
  45. //----------------------------------------------------------------------------------------
  46. // TDemoDocument::DoMakeViews: 
  47. //----------------------------------------------------------------------------------------
  48. #pragma segment MAOpen
  49.  
  50. pascal void TDemoDocument::DoMakeViews(Boolean /*forPrinting*/)
  51. {
  52.     TWindow *aWindow;
  53.     FailInfo    fi;
  54.  
  55.     VOLATILE(aWindow);
  56.     
  57.     if(fi.Try()) {
  58.         aWindow = gViewServer->NewTemplateWindow(kMainView, this);
  59.         fLineCountText = (TNumberText *)aWindow->FindSubView(kNumberOfLines);
  60.         fLineCountText->SetValue(fLineCount, true);
  61.         fi.Success();
  62.     }
  63.     else {
  64.         fLineCountText = (TNumberText *) FreeIfObject(fLineCountText);
  65.         fi.ReSignal();
  66.     }
  67.  
  68. //----------------------------------------------------------------------------------------
  69. // TDemoDocument::DoRead: 
  70. //----------------------------------------------------------------------------------------
  71. #pragma segment MAReadFile
  72.  
  73. pascal void TDemoDocument::DoRead(TFile* aFile, Boolean /*forPrinting*/)
  74. {        
  75.     // Read in the long number of lines we'll be printing
  76.     
  77.     long    lineCount;
  78.     long    bytes = sizeof(long);
  79.  
  80.     aFile->ReadData(&lineCount, bytes);
  81.     fLineCount = lineCount;
  82. //----------------------------------------------------------------------------------------
  83. // TDemoDocument::DoWrite: 
  84. //----------------------------------------------------------------------------------------
  85. #pragma segment MAReadFile
  86. pascal void TDemoDocument::DoWrite(TFile* aFile, Boolean /*makingCopy*/)
  87. {    
  88.     // First, get the current value from the view
  89.  
  90.     long    lineCount = fLineCount = fLineCountText->GetValue();    
  91.     long    bytes = sizeof(long);
  92.     
  93.     aFile->WriteData(&lineCount, bytes);    // and whip it onto the disk    
  94.  
  95. //----------------------------------------------------------------------------------------
  96. // TDemoDocument::DoMenuCommand:
  97. //----------------------------------------------------------------------------------------
  98. #pragma segment MASelCommand
  99. pascal void TDemoDocument::DoMenuCommand(CommandNumber aCommandNumber)
  100. {
  101.     long    lineCount;
  102.     
  103.     switch(aCommandNumber)
  104.     {
  105.         case cPageSetup:
  106.             fReportPrinter->HandlePageSetup();
  107.             break;
  108.     
  109.         case cPrint:
  110.             lineCount = fLineCountText->GetValue();    // get current value
  111.             fReportPrinter->SetDocumentName(fTitle);    
  112.             if(fReportPrinter->StartReport(aCommandNumber)) {
  113.                 fReportPrinter->PrintHeader();
  114.                 
  115.         // Your code to feed lines to the report printer goes here
  116.         // (or better yet, all this should be inside a command)
  117.         // this little example just prints a bunch of lines, all the same
  118.         
  119.                 for(long i=0;i<lineCount;i++) 
  120.                     fReportPrinter->PrintALine(kTheLineToPrint, true);
  121.                 
  122.                 fReportPrinter->EndReport();
  123.             }
  124.             break;
  125.         
  126.         default:
  127.             inherited::DoMenuCommand(aCommandNumber);
  128.             break;
  129.     }
  130. }
  131.  
  132. //----------------------------------------------------------------------------------------
  133. // TDemoDocument::DoSetupMenus: 
  134. //----------------------------------------------------------------------------------------
  135. #pragma segment MAEvtHandlerRes
  136. pascal void TDemoDocument::DoSetupMenus()
  137. {
  138.     inherited::DoSetupMenus();
  139.     Enable(cSave, true);
  140.     Enable(cPageSetup, true);
  141.     Enable(cPrint, true);
  142.